package com.mycompany.app.common.security.acegi;

import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;

import com.mycompany.app.common.security.SecurityConstants;

public class SecurityUtils {

    public static String getCurrentLoginName() {

        String userName = SecurityConstants.ANONYMOUS_USER;

        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof org.acegisecurity.userdetails.UserDetails) {
                com.mycompany.app.common.security.model.User user = (com.mycompany.app.common.security.model.User) principal;
                userName = user.getUsername();
            } else {
                userName = principal.toString();
            }
        }
        return userName;
    }

}
